package com.brillilab.starter.task;

import com.brillilab.service.constant.JobConstants;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.lab.ILabService;
import com.brillilab.service.core.user.IInviteCodeService;
import com.brillilab.service.core.user.IUsersService;
import com.brillilab.service.job.user.MakeInviteCodeJob;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.quartz.BrillQuartzScheduler;
import org.apache.commons.lang.StringUtils;
import org.quartz.JobDataMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zlb
 * @Title: MakeInviteCodeTask
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/10/14 17:42
 */
@Component
@Configuration
@EnableScheduling
public class MakeInviteCodeTask {
    private static Logger logger = LoggerFactory.getLogger(MakeInviteCodeTask.class);

    @Resource
    private BrillQuartzScheduler scheduler;
    @Resource
    private IUsersService usersService;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ILabMemberService labMemberService;
    @Resource
    private ILabService labService;
    @Resource
    private IInviteCodeService inviteCodeService;
    @Resource
    private MessageProducer producer;

    /**
     * 启动邀请码发放定时任务（每月1号0点10分执行一次）
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void startTask() {
        String jobInfo = scheduler.getJobInfo(JobConstants.JOB_GROUP_MAKE_INVITE_CODE, JobConstants.JOB_GROUP_MAKE_INVITE_CODE);
        if(StringUtils.isEmpty(jobInfo)) {
            String cronExp="0 10 0 1 * ?"; //（每月1号0点10分执行一次）
            JobDataMap jobDataMap = new JobDataMap();
            //jobDataMap.put("labId", lab.getId());
            scheduler.createJob(MakeInviteCodeJob.class, JobConstants.JOB_GROUP_MAKE_INVITE_CODE, JobConstants.JOB_GROUP_MAKE_INVITE_CODE, cronExp, jobDataMap);
            logger.info("创建邀请码发放定时任务" + System.currentTimeMillis());
        }
    }
}
